package onlinecalsys.client.view.gui;


 /****************************************************************/ 
 /*                      MainGUI	                            */ 
 /*                                                              */ 
 /****************************************************************/ 
 import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.SystemTray;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

import onlinecalsys.client.view.MainView;
import onlinecalsys.client.view.View;
import onlinecalsys.server.objects.CalsysAppointment;
import onlinecalsys.server.objects.CalsysCalendar;
 /** 
  * Summary description for MainGUI 
  * 
  */ 
 public class MainGUI extends JFrame 
 { 
	
 	// Variables declaration 
 	private JPanel contentPane; 
 	//----- 
 	private JTextArea jTextAreaNotes; 
 	private JScrollPane jScrollPaneNotes; 
 	private JPanel jPanelNotes; 
 	//----- 
 	private JTextArea jTextAreaViewAppointment; 
 	private JScrollPane jScrollPane4; 
 	private JPanel jPanelViewDays; 
 	//----- 
 	private JButton jButtonTomorrow;
 	private JButton jButtonToday;
 	private JButton jButtonYesterday;
 	private JButton jButtonSharedAppointment;
 	private JPanel jPanelDaysButtons; 
 	//----- 
 	private JButton jButtonDeleteAppointment;
 	private JButton jButtonNewAppointment;
 	private JButton jButtonShareAppointment;
 	private JButton	jButtonSaveNotes;
 	private JPanel jPanelAppointmentStafButtons; 
 	//----- 
 	private JPanel jPanel5; 
 	//----- 
 	private JMenuBar menuBar;
 	
 	private JTextArea notestitle;
	private JPanel jPanelnotestitle;
 	// End of variables declaration 
	private View mainView;
	private String actualDate;
	private SharedAppointment sharedApp;
	
  
	
 	public MainGUI(View mainView) 
 	{ 	
 		super(); 
 		this.mainView = mainView;
 		initializeComponent(); 
 		this.setTitle("Online Calender System");
 		
 		actualDate = CalsysCalendar.today();
 		
 		
 	// load frame logo
		BufferedImage image = null;
		try {
			URL u = this.getClass().getResource(
			"../../../../");
			File f = new File(u.getPath().replaceAll("bin/", "images/middle1.png"));
			System.out.println(f.getAbsolutePath());
			image = ImageIO.read(f);
			
			
		} catch (IOException e) {
			new Error("failed loading middle1.png");
		} catch (Exception e) {
			new Error("failed loading middle1.png");
		}
		this.setIconImage(image);
 		
// 		JFrame.setDefaultLookAndFeelDecorated(true); 
// 		JDialog.setDefaultLookAndFeelDecorated(true); 
 		try 
 		{ 
// 			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
 		} 
 		catch (Exception ex) 
 		{ 
 			System.out.println("Failed loading L&F: "); 
 			System.out.println(ex); 
 		} 
 		
 		
  
 		this.setVisible(true); 
 	} 
  
 	/** 
 	 * This method is called from within the constructor to initialize the form. 
 	 * WARNING: Do NOT modify this code. The content of this method is always regenerated 
 	 * by the Windows Form Designer. Otherwise, retrieving design might not work properly. 
 	 * Tip: If you must revise this method, please backup this GUI file for JFrameBuilder 
 	 * to retrieve your design properly in future, before revising this method. 
 	 */ 
 	private void initializeComponent() 
 	{ 
 		contentPane = (JPanel)this.getContentPane(); 
 		//----- 
 		jTextAreaNotes = new JTextArea(); 
 		jScrollPaneNotes = new JScrollPane(); 
 		jPanelNotes = new JPanel(); 
 		//----- 
 		jTextAreaViewAppointment = new JTextArea(); 
 		jScrollPane4 = new JScrollPane(); 
 		jPanelViewDays = new JPanel(); 
 		//----- 
 		jButtonSharedAppointment=new JButton();
 		jButtonTomorrow=new JButton();
 		jButtonToday=new JButton();
 		jButtonYesterday=new JButton();
 		
 		
 		jPanelDaysButtons = new JPanel(); 
 		//----- 
 		jButtonDeleteAppointment = new JButton();
 		jButtonNewAppointment = new JButton();
 		jButtonShareAppointment = new JButton();
 		jButtonSaveNotes = new JButton();
 		jPanelAppointmentStafButtons = new JPanel(); 
 		//----- 
 		jPanel5 = new JPanel();
 		jPanelnotestitle = new JPanel();
 		
 		notestitle = new JTextArea();
 		
 		//----- 
 		
 		notestitle.setText("Note");
 		notestitle.setEditable(true);
 		notestitle.setDragEnabled(false);
 		notestitle.setEnabled(false);
 		
 		jPanelnotestitle.add(notestitle);
 		
  
 		// 
 		// contentPane 
 		// 
 		contentPane.setLayout(new BorderLayout(0, 0)); 
 		contentPane.add(jPanelNotes, BorderLayout.WEST); 
 		contentPane.add(jPanelViewDays, BorderLayout.CENTER); 
 		// 
 		// jTextArea5 
 		// 
 		jTextAreaNotes.setText("               "); 
 		// 
 		// jScrollPaneNotes 
 		// 
 		jScrollPaneNotes.setViewportView(jTextAreaNotes); 
 		// 
 		// jPanelNotes 
 		// 
 		
 		jPanelNotes.setLayout(new BorderLayout(0, 0));
 		jPanelNotes.setBorder(new TitledBorder("Note"));
 		jPanelNotes.add(jPanel5, BorderLayout.SOUTH); 
 		jPanelNotes.add(jScrollPaneNotes, BorderLayout.CENTER); 
 		//jPanelNotes.add(jPanelnotestitle, BorderLayout.NORTH);
 		// 
 		// jTextArea4 
 		// 
 		jTextAreaViewAppointment.setEditable(false); 
 		// 
 		// jScrollPane4 
 		// 
 		jScrollPane4.setViewportView(jTextAreaViewAppointment); 
 		// 
 		// jPanelViewDays 
 		// 
 		jPanelViewDays.setLayout(new BorderLayout(0, 0)); 
 		jPanelViewDays.add(jPanelDaysButtons, BorderLayout.NORTH); 
 		jPanelViewDays.add(jPanelAppointmentStafButtons, BorderLayout.SOUTH); 
 		jPanelViewDays.add(jScrollPane4, BorderLayout.CENTER); 
 		//jPanelViewDays.setBorder(BorderFactory.createLoweredBevelBorder()); 
 		// 
 		// jPanelDaysButtons 
 		// 
 		
 		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 		jButtonTomorrow.setText("NextDay"); 
 		jButtonTomorrow.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButtonTomorrow_actionPerformed(e); 
 			} 
  
 		}); 
 		jButtonToday.setText("Today"); 
 		jButtonToday.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButtonToday_actionPerformed(e); 
 			} 
  
 		}); 
 		jButtonYesterday.setText("Previous Day"); 
 		jButtonYesterday.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButtonYesterday_actionPerformed(e); 
 			} 
  
 		}); 
 		
 		jButtonSharedAppointment.setText("SharedAppointment"); 
 		jButtonSharedAppointment.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButtonSharedAppointment_actionPerformed(e); 
 			} 
  
 		});
 		
 	//...........................................
// 		jPanelDaysButtons.setLayout(new BoxLayout(jPanelDaysButtons, BoxLayout.X_AXIS)); 
 		jPanelDaysButtons.setLayout(new FlowLayout()); 
 		jPanelDaysButtons.setBorder(new TitledBorder("Calendar")); 
 		
 		JLabel label1 = new JLabel();
 		label1.setText("wellow");
 		
 		jPanelDaysButtons.add(new Icon());
 		jPanelDaysButtons.add(jButtonYesterday);
 		jPanelDaysButtons.add(jButtonToday);
 		jPanelDaysButtons.add(jButtonTomorrow);
 		jPanelDaysButtons.add(jButtonSharedAppointment);
 		jPanelDaysButtons.add(new Icon());
 		
 		
 		// 
 		// jButtonDeleteAppointment 
 		// 
 		jButtonDeleteAppointment.setText("Delete"); 
 		jButtonDeleteAppointment.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButtonDeleteAppointment_actionPerformed(e); 
 			} 
  
 		}); 
 		// jButtonNewAppointment 
 		// 
 		jButtonNewAppointment.setText("New"); 
 		jButtonNewAppointment.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButtonNewAppointment_actionPerformed(e); 
 			} 
  
 		});
 		// jButtonShareAppointment 
 		// 
 		jButtonShareAppointment.setText("Share"); 
 		jButtonShareAppointment.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButtonShareAppointment_actionPerformed(e); 
 			} 
  
 		});
 		
 		jButtonSaveNotes.setText("Save note"); 
 		jButtonSaveNotes.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButtonSaveNotes_actionPerformed(e); 
 			} 
  
 		});
 	
 		// 
 		// jPanelAppointmentStafButtons 
 		// 
 		jPanelAppointmentStafButtons.setLayout(new FlowLayout()); 
 		jPanelAppointmentStafButtons.add(jButtonDeleteAppointment); 
 		jPanelAppointmentStafButtons.add(jButtonNewAppointment);
 		jPanelAppointmentStafButtons.add(jButtonShareAppointment);
 		jPanelAppointmentStafButtons.setBorder(new TitledBorder("Calendar Actions")); 
 		// 
 		// jPanel5 
 		// 
 		jPanel5.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
 		jPanel5.add(jButtonSaveNotes);
 		jPanel5.setBorder(new TitledBorder("Note Actions")); 
 		// 
 		// MainGUI 
 		// 
 		this.setTitle("Online Calendar System"); 
 		this.setLocation(new Point(0, 0)); 
 		this.setSize(new Dimension(736, 571)); 
 		this.setResizable(false); 
 		
 		
        createMenuBar();
 	}

	private void createMenuBar() {
		// Creates a menubar for a JFrame
 		menuBar = new JMenuBar();
        
        // Add the menubar to the frame
        setJMenuBar(menuBar);
        
        // Define and add two drop down menu to the menubar
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");
       
        menuBar.add(fileMenu);
        menuBar.add(helpMenu);
        
        
        // Create and add simple menu item to one of the drop down menu
        JMenuItem previousDayAction = new JMenuItem("Previous Day");
        JMenuItem todayAction = new JMenuItem("Today");
        JMenuItem nextDayAction = new JMenuItem("Next Day");
        JMenuItem SharedAppointmentAction = new JMenuItem("Shared Appointment");
        JMenuItem saveNotesAction = new JMenuItem("Save Notes");
        JMenuItem shareAction = new JMenuItem("Share Appointment");
        JMenuItem newAction = new JMenuItem("New Appointment");
        JMenuItem deleteAction = new JMenuItem("Delete Appointment");
        JMenuItem exitAction = new JMenuItem("Exit");
        
        JMenuItem aboutAction = new JMenuItem("About");
       
     
        

    //    ButtonGroup bg = new ButtonGroup();
       
        fileMenu.add(previousDayAction);
        fileMenu.add(todayAction);
        fileMenu.add(nextDayAction);
        fileMenu.add(SharedAppointmentAction);
        fileMenu.addSeparator();
        fileMenu.add(saveNotesAction);
        fileMenu.addSeparator();
        fileMenu.add(shareAction);
        fileMenu.add(newAction);
        fileMenu.add(deleteAction);
        fileMenu.addSeparator();
        fileMenu.add(exitAction);
        
        helpMenu.add(aboutAction);
    
        // Add a listener to the New menu item. actionPerformed() method will
        // invoked, if user triggred this menu item
        aboutAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	new AboutInfo().main(null);
            }
        });
        
        exitAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	System.exit(0);
            }
        });
        previousDayAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	try {
        			actualDate = CalsysCalendar.getDayBefore(actualDate);
        			mainView.updateAppoinments(actualDate);
        		} catch (ParseException e1) {
        			// TODO Auto-generated catch block
        			e1.printStackTrace();
        		}
            }
        });
        todayAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            
            	mainView.updateAppoinments(CalsysCalendar.today());
         		actualDate = CalsysCalendar.today();
            	
            }
        });
        nextDayAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	try {
        			actualDate = CalsysCalendar.getNextDay(actualDate);
        			mainView.updateAppoinments(actualDate);
        		} catch (ParseException e1) {
        			// TODO Auto-generated catch block
        			e1.printStackTrace();
        		}
            }
        });
        
        SharedAppointmentAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	
            	mainView.getSharedAppointments();
            	
            	System.out.println("You have clicked on the SharedAppointment");
            }
        });
        
        saveNotesAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	
            	String note = jTextAreaNotes.getText();
        		mainView.saveNote(note);
               
            }
        });
        shareAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	
            	ShareAppointment  share = new ShareAppointment(mainView);
            	
                System.out.println("You have clicked on the shareAction");
            }
        });
        newAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	
            	SaveAppointment save = new SaveAppointment(mainView);
                
            	System.out.println("You have clicked on the new action");
            }
        });
        deleteAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	
            	DeleteAppointment delete = new DeleteAppointment(mainView, actualDate);
            	
                System.out.println("You have clicked on the deleteAction");
            }
        });
	} 
  
 	// 
 	// TODO: Add any appropriate code in the following Event Handling Methods 
 	// 
 	private void jButtonDeleteAppointment_actionPerformed(ActionEvent e) 
 	{ 
 		
 		DeleteAppointment delete = new DeleteAppointment(mainView, actualDate); 		
 		
 		System.out.println("\njButtonDeleteAppointment_actionPerformed(ActionEvent e) called."); 
 		// TODO: Add any handling code here 
  
 	} 
 	private void jButtonNewAppointment_actionPerformed(ActionEvent e) 
 	{ 
 		SaveAppointment save = new SaveAppointment(mainView);
 		System.out.println("This");
 		
  
 	} 
 	private void jButtonShareAppointment_actionPerformed(ActionEvent e) 
 	{ 
 		ShareAppointment  share = new ShareAppointment(mainView);
 		
  
 	} 
 	
 	private void jButtonTomorrow_actionPerformed(ActionEvent e) 
 	{ 
 		try {
			actualDate = CalsysCalendar.getNextDay(actualDate);
			mainView.updateAppoinments(actualDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
  
 	} 
 	private void jButtonToday_actionPerformed(ActionEvent e) 
 	{ 
 		mainView.updateAppoinments(CalsysCalendar.today());
 		actualDate = CalsysCalendar.today();
 	} 
 	private void jButtonYesterday_actionPerformed(ActionEvent e) 
 	{ 
 		try {
			actualDate = CalsysCalendar.getDayBefore(actualDate);
			mainView.updateAppoinments(actualDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
  
 	} 
	private void jButtonSharedAppointment_actionPerformed(ActionEvent e) 
 	{ 	
		jButtonSharedAppointment.setForeground(Color.black);
		mainView.getSharedAppointments();
  
 	} 
	private void jButtonSaveNotes_actionPerformed(ActionEvent e) 
 	{ 
		
		String note = jTextAreaNotes.getText();
		mainView.saveNote(note);
		
		
 	}
	
	public void setTextAppointment(String appointments) {
		jTextAreaViewAppointment.setText(appointments);
	}
	
	

	public void setNote(String note) {
		jTextAreaNotes.setText(note);
	}

	public void newSharedAppointments(String app) {
		sharedApp = new SharedAppointment();
		sharedApp.setText(app);
		
	}

	public void setNewSharedAppointmentes() {
		jButtonSharedAppointment.setForeground(Color.RED);
	}
  
 	// 
 	// TODO: Add any method code to meet your needs in the following area 
 	// 
  
  //============================= Testing ================================// 
 //=                                                                    =// 
 //= The following main method is just for testing this class you built.=// 
 //= After testing,you may simply delete it.                            =// 
 //======================================================================// 
// 	public static void main(String[] args) 
// 	{ 
// 		JFrame.setDefaultLookAndFeelDecorated(true); 
// 		JDialog.setDefaultLookAndFeelDecorated(true); 
// 		try 
// 		{ 
// 			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
// 		} 
// 		catch (Exception ex) 
// 		{ 
// 			System.out.println("Failed loading L&F: "); 
// 			System.out.println(ex); 
// 		} 
// 		new MainGUI(null); 
// 	} 
// //= End of Testing = 
  
  
 } 
  
 